package com.xm.xmapp.test;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.xm.xmapp.R;

public class DrawButton extends View {

	float index = 0;

	float minOnclick;

	float maxIndex;
	/** 属性动画 */
	private ObjectAnimator animator;
	private Bitmap bitmap, bitmapJian, bitmapMove;

	private Paint paint = new Paint();

	Paint _paint_text = new Paint();

	public DrawButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		bitmap = BitmapFactory.decodeResource(this.getContext().getResources(),
				R.mipmap.car3);

		bitmapMove = BitmapFactory.decodeResource(this.getContext()
				.getResources(), R.mipmap.car4);

		bitmapJian = BitmapFactory.decodeResource(this.getContext()
				.getResources(), R.mipmap.huadong);

		float size = getResources().getDimension(R.dimen.smallsize2);
		_paint_text.setTextSize(size);
		_paint_text.setColor(Color.parseColor("#ff666666"));

		_paint_text.setTypeface(Typeface.DEFAULT);

	}

	private int indexJianTou;

	public int getIndexJianTou() {
		return indexJianTou;
	}

	public void setIndexJianTou(int indexJianTou) {
		this.indexJianTou = indexJianTou;
		invalidate();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int with = (int) getResources().getDimension(R.dimen.dp204);
		int height = (int) getResources().getDimension(R.dimen.dp50);
		if (bitmap != null)
			height = bitmap.getHeight() + getPaddingTop() + getPaddingBottom();

		maxIndex = with - bitmap.getWidth() - getPaddingLeft()
				- getPaddingRight();

		minOnclick = bitmap.getWidth() + getPaddingLeft();

		setMeasuredDimension(with, height);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		if (index == 0) {

			canvas.drawBitmap(bitmap, index + getPaddingLeft(),
					getPaddingTop(), paint);

			int left = bitmap.getWidth() + 2 * getPaddingLeft();

			canvas.drawBitmap(bitmapJian, left,
					(bitmap.getHeight() - bitmapJian.getHeight()) / 2
							+ getPaddingTop(), paint);

			float textHeight = -(_paint_text.ascent() + _paint_text.descent());
			canvas.drawText("开始用车之旅", left + bitmapJian.getWidth()
					+ getPaddingLeft(), getHeight() / 2 + textHeight / 2,
					_paint_text);
		} else {
			canvas.drawBitmap(bitmapMove, index + getPaddingLeft(),
					getPaddingTop(), paint);
		}

	}

	boolean isDraw;
	float downX;

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:

			if (event.getX() <= minOnclick) {
				isDraw = true;
				downX = event.getX();

			} else {
				isDraw = false;
				downX = 0;

			}
			break;
		case MotionEvent.ACTION_MOVE:

			if (isDraw) {

				index = (event.getX() - downX);

				if (index > maxIndex)
					index = maxIndex;

				if (index < 0)
					index = 0;

				invalidate();

			}

			break;
		case MotionEvent.ACTION_UP:

			if (isDraw) {
				if (index < maxIndex) {
					downX = 0;
					animator = ObjectAnimator.ofFloat(this, "index", index, 0);
					animator.setDuration((int) 500);
					animator.start();
				} else {
					animator = ObjectAnimator.ofFloat(this, "index", index, 0);
					animator.setDuration((int) 500);
					animator.start();
					l.onClick(this);

				}

			}

			break;

		default:
			break;
		}

		return true;
	}

	OnClickListener l;

	@Override
	public void setOnClickListener(OnClickListener l) {
		// TODO Auto-generated method stub
		// super.setOnClickListener(l);
		this.l = l;
	}

	@Override
	public void setVisibility(int visibility) {
		// TODO Auto-generated method stub
		super.setVisibility(visibility);

		if (visibility == View.GONE || visibility == View.INVISIBLE) {
			setIndex(0);
		} else {

		}

	}

	public void setIndex(float index) {

		this.index = index;
		invalidate();
	}

}
